<template>
    <div class="robot-management" v-loading="loading">
        <div class="input-box">
            <span class="input-title" style="margin-left:0px;">名称</span>
            <el-input
                placeholder="搜索客服机器人名称"
                @keyup.enter.native="selectChange"
                style="width:280px;"
                clearable
                v-model.trim="paramsList.keyWord">
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>
            <span class="input-title">状态</span>
            <el-select @change="selectChange" style="width:150px;" v-model="paramsList.status">
                <el-option label="全部" :value="null"></el-option>
                <el-option label="使用" :value="0"></el-option>
                <el-option label="封禁" :value="2"></el-option>
                <el-option label="停用" :value="-1"></el-option>
                <el-option label="满员" :value="-2"></el-option>
            </el-select>
            <el-button type="primary" class="btn" style="margin-left:20px" @click="getList(1, 10)">查询</el-button>
            <el-button type="success" class="btn" style="margin-left:20px" @click="upOrCreateTrigger(1)">新建</el-button>
        </div>
        <div class="table-box">
            <el-table :data="tableData" ref="table" border style="width: 100%">
                <el-table-column fixed width="160" prop="orderNo" label="客服机器人"></el-table-column>
                <el-table-column min-width="100" label="二维码">
                    <template slot-scope="scope">
                        <imgBox :url="scope.row.thumbnail" :width="80"/>
                    </template>
                </el-table-column>
                <el-table-column width="160" prop="orderNo" label="状态"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button @click="upOrCreateTrigger(0)" type="text" size="small">编辑</el-button>
                        <el-button @click="delRobot(scope.row)" type="text" size="small">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
        <upOrCreateDialog :title="dialogTitle" :dialogVisible="dialogVisible" :submit="upOrCreateSubmit" />
    </div>
</template>

<script>
import upOrCreateDialog from './component/upOrCreateDialog'
export default {
    components: {
        upOrCreateDialog
    },
    data () {
        return {
            paramsList: {
                keyWord: '',
                status: null
            },
            pageNum: 1,
            pageSize: this._pageSize,
            dialogTitle: '编辑信息',
            date: [],
            tableData: [],
            dialogParams: {},
            total: 0,
            loading: false,
            aaa: true,
            shijuan: true,
            dialogVisible: false
        }
    },
    created () {
        this.getList(1)
    },
    methods: {
        // 获取列表
        async getList (pageNum) {
            if (this.loading) return
            this.pageNum = pageNum
            this.loading = true
            let params = {
                pageNum,
                pageSize: this.pageSize
            }
            if (this.keyWord) {
                params.keyWord = this.keyWord
            }

            if (this.date && this.date.length) {
                params.fromDate = this.date[0] + ' 00:00:00'
                params.endDate = this.date[1] + ' 23:59:59'
            }

            let res = await this.$http.get('/trade/selectTradeOrderDetail', {
                params
            })
            if (res.code === '0') {
                this.tableData = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },
        upOrCreateTrigger (type) {
            if (type) this.dialogTitle = '添加客服机器人'
            this.dialogVisible = true
        },
        upOrCreateSubmit ({ params }) {
            this.dialogVisible = false
            if (params) {
                console.log(params)
            }
        },
        delRobot (item) {
            this.$confirm(`删除后，机器人${item}将无法找回，请谨慎操作。确认删除？`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
            })
        },
        // 页码切换
        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getList(1)
        },
        // 页容切换
        pageChange (val) {
            this.pageNum = val
            this.getList(this.pageNum)
        },
        selectChange () {
            this.type = ''
            this.sort = ''
            this.$refs.table.clearSort()
            this.getList(1, 10)
        }
    }
}
</script>

<style lang="scss" scoped>
    .robot-management {
        .input-box {
            margin-top: 10px;
            font-size: 13px;
            font-weight: 500;
            .input-title {
                margin: 0 10px 0 20px;
            }
            .btn {
                margin-top: 10px;
                margin-left: 10px;
            }
        }

        .table-box {
            margin: 20px 0;
        }

        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
